package com.party.service.exam.impl;

import com.party.mapper.User2ExamMapper;
import com.party.mapper.User2ExamtestMapper;
import com.party.model.Exam;
import com.party.model.ExamAnswer;
import com.party.model.User2Exam;
import com.party.model.User2Examtest;
import com.party.service.exam.ExamAnswerService;
import com.party.service.exam.User2ExamService;
import com.party.service.exam.User2ExamTestService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-9
 * Time: 下午8:04
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class User2ExamTestServiceImpl implements User2ExamTestService {

    @Resource
    User2ExamService user2ExamService;
    @Resource
    User2ExamtestMapper user2ExamtestMapper;
    @Resource
    ExamAnswerService examAnswerService;

    @Override
    public int add(List<User2Examtest> user2ExamtestList,Integer preScore) {
        if(user2ExamtestList != null && user2ExamtestList.size() >0)
        {
            int addScore = 0;//加上的分数
            for(User2Examtest obj:user2ExamtestList)
            {
                boolean right = this.dealSelect(obj);
                if(right)
                {
                    addScore = addScore +preScore;
                }
                this.user2ExamtestMapper.insertSelective(obj);
            }

            if(addScore > 0)
            {
                //给user2Curriculum加分
                User2Exam user2Exam = this.user2ExamService.get(user2ExamtestList.get(0).getUid(),user2ExamtestList.get(0).getExamid());
                if(user2Exam != null)
                {
                    Integer score = user2Exam.getScore();
                    if(score==null || score <0)
                    {
                        score =0;
                    }
                    score = score+addScore;//加上得分
                    user2Exam.setScore(score);
                    this.user2ExamService.update(user2Exam);
                }
            }
            return user2ExamtestList.size();
        }
        return 0;
    }

    /**
     * 看这个题选择是否正确
     * @return true正确，false错误
     */
    private boolean dealSelect(User2Examtest obj)
    {
        boolean isRight = false;
        if(obj !=null) {
//            Test test = this.testService.getById(obj.getTestid());
            //判断这个题目选对了没有
            List<ExamAnswer> answerList = this.examAnswerService.getByTestId(obj.getExamtestid());
            isRight = true;
            if (answerList != null) {
                String[] selects = obj.getAnswerList();
                for (ExamAnswer answer : answerList) {
                    String id = answer.getId() + "";
                    String right = answer.getIsright();//1是正确选项，0不是
                    boolean in = false;
                    if(selects != null && selects.length >0)
                    {
                        for (String select : selects) {
                            //确保每个正确答案都选了
                            if (right.equals("1") && select.equals(id)) {
                                in = true;
                            }
                            //这个选项用户选了的
                            if (select.equals(id) && right.equals("0")) {
                                isRight = false;
                                break;
                            }
                        }
                        //这个选项是对的，但是又没有选，直接判错
                        if (right.equals("1") && !in) {
                            isRight = false;
                            break;
                        }
                    }
                    else{
                        isRight = false;
                        break;
                    }
                }
            }
            //这个题做对了
            if (isRight) {
                obj.setIsright(1);
            } else {
                obj.setIsright(0);
            }
        }
        return isRight;
    }
}
